package son.hut.vn.app.utility;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * NetworkUtility checks available network
 * xu li trang thai cua mang
 * 
 * @author sonnv6207
 */
public final class SmartNetworkUtility
{
    private Context context = null;
    
    private static SmartNetworkUtility instance = null;
    
    /**
     * Constructor
     * 
     * @param context
     */
    private SmartNetworkUtility(Context context)
    {
        this.context = context;
    }
    /**
     * Get class instance
     * 
     * @param context
     * @return
     */
    public static SmartNetworkUtility getInstance(Context context)
    {
        if (instance == null)
        {
            instance = new SmartNetworkUtility(context);
        }
        return instance;
    }
    
    /**
     * Check network connection
     * 
     * @return
     */
    public boolean isNetworkAvailable()
    {
        ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo i = conMgr.getActiveNetworkInfo();
        if (i == null)
        {
            return false;
        }
        else if (!i.isConnected()) 
        {
            return false;
        }
        else if (!i.isAvailable()) {
            return false;
        }
        return true;
    }
}
